package com.furniture.ui.activity;

import android.content.Context;
import android.content.SharedPreferences;
import android.os.Handler;
import android.support.design.widget.NavigationView;
import android.support.v4.widget.DrawerLayout;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;

import com.furniture.R;
import com.furniture.ui.utils.CircleTransformation;
import com.squareup.picasso.Picasso;

import butterknife.Bind;
import butterknife.BindDimen;
import butterknife.BindString;

/**
 * Created by Bob on 2016/3/27.
 */
public class BaseDrawerActivity extends BaseActivity {

    public static final String LOGIN_USER_NAME_TYPE = "login_user_name_type";
    public static final int TAB_ONE_IS_SELECTED = 0;
    public static final int TAB_TWO_IS_SELECTED = 1;
    public static final int TAB_THREE_IS_SELECTED = 2;

    @Bind(R.id.drawerLayout)
    DrawerLayout drawerLayout;
    @Bind(R.id.vNavigation)
    NavigationView vNavigation;

    @BindDimen(R.dimen.global_menu_avatar_size)
    int avatarSize;
    @BindString(R.string.user_profile_photo)
    String profilePhoto;

    private Context mContext;

    //Cannot be bound via ButterKnife, hosting view is initialized later (see setupHeader() method)
    private ImageView ivMenuUserProfilePhoto;

    @Override
    public void setContentView(int layoutResID) {
        super.setContentViewWithoutInject(R.layout.activity_drawer);
        ViewGroup viewGroup = (ViewGroup) findViewById(R.id.flContentRoot);
        LayoutInflater.from(this).inflate(layoutResID, viewGroup, true);
        mContext = this;
        bindViews();
        setupHeader();
//      获取用户名
        SharedPreferences sp = getSharedPreferences("mySp", Context.MODE_PRIVATE);
        if (sp!=null){
            String name = sp.getString(LOGIN_USER_NAME_TYPE,"");
            menuUserName.setText(name);
        }
    }

    @Override
    protected void setupToolbar() {
        super.setupToolbar();
        if (getToolbar() != null) {
            getToolbar().setNavigationOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    drawerLayout.openDrawer(Gravity.LEFT);
                }
            });
        }
    }

    TextView menuUserName;
    ImageButton ivSearch;
    Menu mMenu;

    private void setupHeader() {
        View headerView = vNavigation.getHeaderView(0);
        mMenu = vNavigation.getMenu();
        ivMenuUserProfilePhoto = (ImageView) headerView.findViewById(R.id.ivMenuUserProfilePhoto);
        menuUserName = (TextView) headerView.findViewById(R.id.menuUserName);

        headerView.findViewById(R.id.vGlobalMenuHeader).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onGlobalMenuHeaderClick(v);
            }
        });

        Picasso.with(this)
                .load(profilePhoto)
                .placeholder(R.drawable.img_circle_placeholder)
                .resize(avatarSize, avatarSize)
                .centerCrop()
                .transform(new CircleTransformation())
                .into(ivMenuUserProfilePhoto);

//        mMenu.findItem()

        vNavigation.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
            @Override
            public boolean onNavigationItemSelected(MenuItem item) {
                int[] startingLocation = new int[2];
                drawerLayout.closeDrawer(Gravity.LEFT);
                switch (item.getItemId()) {
                    case R.id.menu_good:
                        UserProfileActivity.startUserProfileFromLocation(startingLocation, BaseDrawerActivity.this, TAB_ONE_IS_SELECTED);
                        break;
                    case R.id.menu_direct:
                        UserProfileActivity.startUserProfileFromLocation(startingLocation, BaseDrawerActivity.this, TAB_TWO_IS_SELECTED);
                        break;
                    case R.id.menu_news:
                        UserProfileActivity.startUserProfileFromLocation(startingLocation, BaseDrawerActivity.this, TAB_THREE_IS_SELECTED);
                        break;
                }
                return false;
            }
        });
    }

    public NavigationView getNavigation(){
        return vNavigation;
    }

    public void onGlobalMenuHeaderClick(final View v) {
        drawerLayout.closeDrawer(Gravity.LEFT);
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                int[] startingLocation = new int[2];
                v.getLocationOnScreen(startingLocation);
                startingLocation[0] += v.getWidth() / 2;
                UserProfileActivity.startUserProfileFromLocation(startingLocation, BaseDrawerActivity.this,TAB_ONE_IS_SELECTED);
                overridePendingTransition(0, 0);
            }
        }, 200);
    }

}
